app.controller('taskscontroller', ['$scope', '$parse', '$http', function($scope, $parse, $http)
{
	$scope.$on('currentUser', function(event, args) {
		$scope.currentUser = args.user;
		$scope.getTasksCommand();
	});

	/**
	* Add a new task
	*/
	$scope.addTaskCommand = function (taskname) 
	{
		console.info("addTaskCommand - called"); 
		
		$scope.loadingTasks = true;
		$scope.taskInput = "";
		console.info("addTaskCommand - call addTaskClient with taskname : " + taskname); 
	
		$http.post($scope.servicebase, { "service" : "addTask", "name" : taskname, "user" : $scope.assigneeUser}).
		success(function(data, status) 
		{
			console.info("addTaskCommand - call success"); 
			$scope.status = status;
			$scope.result = data;
			console.info(JSON.stringify(data));
			if(data.isFailed == true)
			{
				$scope.setMessageCommand(data.exception, 'danger');
			}
			else
			{
				$scope.tasks.push(data.response);
				$scope.addNewClicked = false;
				$scope.setMessageCommand('Tâche ajoutée', 'success');
				$scope.loadingTasks = false;
			}
		})
		.
		error(function(data, status) 
		{
			console.error("addTaskCommand - call failed"); 
			$scope.data = data || "Request failed";
			$scope.status = status;			
			$scope.setMessageCommand('Ajout de la tache en échec', 'danger');
			$scope.loadingTasks = false;
			console.error("addTaskCommand - data : " + $scope.data); 		
		});
	};
	
	/**
	* delete a task command
	*/
	$scope.deleteTaskCommand = function(id)
	{
		console.info("deleteTaskCommand - called"); 
		
		$scope.loadingTasks = true;
		console.info("deleteTaskCommand - call deleteTaskClient with id : " + id); 
		
		$http.post($scope.servicebase, { "service" : "deleteTask", "id" : id}).
		success(function(data, status)
		{
			console.info("deleteTaskCommand - call success"); 
			$scope.status = status;
			$scope.data = data;
			$scope.result = data;
			
			if(data.isFailed == true)
			{
				$scope.setMessageCommand($scope.data.exception, 'danger');
			}
			else
			{
				$scope.loadingTasks = false;
				$scope.getTasksCommand();
				$scope.setMessageCommand('Tâche supprimée', 'success');
			}
		})
		.
		error(function(data, status) 
		{
			console.error("getTasksCommand - call failed"); 
			$scope.loadingDocuments = false;
			$scope.data = data || "Request failed";
			$scope.status = status;			 	
			$scope.setMessageCommand('Suppression de la tache en échec', 'danger');
			console.error("getTasksCommand - data : " + $scope.data); 			
		});
	};
	
	/**
	* Update status command
	*/
	$scope.updateStatusCommand = function(id, status, task) 
	{
		console.info("updateStatusCommand - called"); 
		
		$scope.loadingTasks = true;
		if(status=='1')
		{
			status='0';
		}
		else
		{
			status='1';
		}
		console.info("updateStatusCommand - call updateTaskClient with id : " + id + " status " + status); 
	
		$http.post($scope.servicebase, { "service" : "updateTask", "id" : id, "status" : status}).
		success(function(data, status) 
		{
			console.info("updateStatusCommand - call success"); 
			$scope.status = status;
			$scope.data = data;
			$scope.result = data;
			if(data.isFailed == true)
			{
				$scope.setMessageCommand($scope.data.exception, 'danger');
			}
			else
			{
				$scope.loadingTasks = false;
				$scope.getTasksCommand();
				$scope.setMessageCommand('Tâche modifiée avec succès', 'success');
			}
		})
		.
		error(function(data, status) 
		{
			console.error("updateStatusCommand - call failed"); 
			$scope.loadingDocuments = false;
			$scope.data = data || "Request failed";
			$scope.status = status;		
			$scope.setMessageCommand('Modification de la tâche en échec', 'danger');
			console.error("updateStatusCommand - data : " + $scope.data); 				
		});
	};
	
	/**
	*	Get all users
	*/
	$scope.getUsersCommand = function()
	{
		console.info("getUsersCommand - called"); 
		$scope.loadingUsers = true;

		$http.post($scope.servicebase, { "service" : "getUsers"}).
		success(function(data, status) 
		{
			console.info("getUsersCommand - call success"); 
			$scope.status = status;
			$scope.data = data;
			$scope.result = data; 
			$scope.loadingUsers = false;
			if(data.isFailed == true)
			{
				$scope.setMessageCommand($scope.data.exception, 'danger');
			}
			
			$scope.users = $scope.data.response;
		})
		.
		error(function(data, status) 
		{
			console.error("getUsersCommand - call failed"); 	
			$scope.loadingUsers = false;
			$scope.data = data || "Request failed";
			$scope.status = status;			
			console.error("getUsersCommand - data : " + $scope.data); 				
		});
	};
	
	/**
	*	Get all active tasks
	*/
	$scope.getTasksCommand = function()
	{
		console.info("getTasksCommand - called"); 
		$scope.loadingTasks = true;
		$scope.tasks = [];
	
		$http.post($scope.servicebase, { "service" : "getTasks", "user" : $scope.currentUser.Id }).
		success(function(data, status) 
		{
			console.info("getTasksCommand - call success"); 
			$scope.status = status;
			$scope.data = data;
			$scope.result = data; 
			$scope.loadingTasks = false;
			if(data.isFailed == true)
			{
				$scope.setMessageCommand($scope.data.exception, 'danger');
			}

			$scope.tasks = $scope.data.response;
		})
		.
		error(function(data, status) 
		{
			console.error("getUsersCommand - call failed"); 	
			$scope.loadingTasks = false;
			$scope.data = data || "Request failed";
			$scope.status = status;			
			console.error("getUsersCommand - data : " + $scope.data); 				
		});
	};
	
	$scope.getUsersCommand();
}]);